package com.wei.czz.framework.blog.manager;

import com.wei.czz.common.constant.EntityConstant;
import com.wei.czz.common.dto.blog.audit.group.BlogAuditGroupDto;
import com.wei.czz.common.dto.blog.audit.group.BlogAuditGroupPageEnumDto;
import com.wei.czz.common.enums.ResultEnum;
import com.wei.czz.common.enums.blog.audit.AuditLevelEnum;
import com.wei.czz.common.enums.blog.audit.AuditTypeEnum;
import com.wei.czz.common.exception.CzzException;
import com.wei.czz.common.tool.PageDto;
import com.wei.czz.common.utils.CacheKeyUtils;
import com.wei.czz.common.utils.CopyUtils;
import com.wei.czz.common.vo.blog.audit.group.BlogAuditGroupFormVo;
import com.wei.czz.common.vo.blog.audit.group.BlogAuditGroupVo;
import com.wei.czz.framework.blog.entity.BlogAuditGroupEntity;
import com.wei.czz.framework.blog.entity.BlogAuditGroupUserEntity;
import com.wei.czz.framework.blog.service.BlogAuditGroupService;
import com.wei.czz.framework.blog.service.BlogAuditGroupUserService;
import com.wei.czz.framework.common.helper.CacheHelper;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-12-05 09:56:12
 * className: BlogAuditGroupManager
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class BlogAuditGroupManager {

    private static final Logger log = LoggerFactory.getLogger(BlogAuditGroupManager.class);

    private final BlogAuditGroupService blogAuditGroupService;

    private final BlogAuditGroupUserService blogAuditGroupUserService;

    private final CacheHelper cacheHelper;

    @Transactional
    public void saveBlogAuditGroup(BlogAuditGroupVo blogAuditGroupVo) {

        /*
            获取博客审核分组
         */
        List<BlogAuditGroupEntity> blogAuditGroupList =  blogAuditGroupService.findList(
                Collections.singletonList(blogAuditGroupVo.getAuditType()),
                Collections.singletonList(blogAuditGroupVo.getAuditLevel())
        );
        if (!blogAuditGroupList.isEmpty()) {
            throw new CzzException(ResultEnum.REFRESH.getCode(), "博客审核分组已存在，请勿重复添加");
        }

        /*
            保存
         */
        BlogAuditGroupEntity blogAuditGroup = new BlogAuditGroupEntity();
        blogAuditGroup.setName(blogAuditGroupVo.getName())
                .setAuditType(blogAuditGroupVo.getAuditType())
                .setAuditLevel(blogAuditGroupVo.getAuditLevel());
        blogAuditGroupService.saveOrEdit(blogAuditGroup);

    }

    public PageDto<BlogAuditGroupDto> getBlogAuditGroupPageList(BlogAuditGroupFormVo formVo) {

        // 缓存页面参数
        String cacheKey = CacheKeyUtils.getParamCacheKey(EntityConstant.BLOG_AUDIT_GROUP);
        cacheHelper.cache(cacheKey, formVo);

        /*
            分页获取博客审核分组
         */
        PageDto<BlogAuditGroupEntity> pageDto = blogAuditGroupService.getBlogAuditGroupPageList(formVo);
        if (pageDto.isEmpty()) {
            return pageDto.rebuild();
        }
        List<BlogAuditGroupEntity> blogAuditGroupList = pageDto.getList();

        // 收集主键
        List<Long> idList = CopyUtils.mapList(blogAuditGroupList, BlogAuditGroupEntity::getId);

        /*
            获取博客审核分组用户
         */
        List<BlogAuditGroupUserEntity> blogAuditGroupUserList = blogAuditGroupUserService.findList(idList);
        Map<Long, Long> blogAuditGroupUserCountMap = blogAuditGroupUserList.stream()
                .collect(Collectors.groupingBy(BlogAuditGroupUserEntity::getGroupId, Collectors.counting()));

        BiConsumer<BlogAuditGroupEntity, BlogAuditGroupDto> consumer = (blogAuditGroup, blogAuditGroupDto) -> {
            blogAuditGroupDto.setAuditTypeName(AuditTypeEnum.getName(blogAuditGroup.getAuditType()))
                    .setAuditLevelName(AuditLevelEnum.getName(blogAuditGroup.getAuditLevel()))
                    .setUserCount(blogAuditGroupUserCountMap.getOrDefault(blogAuditGroup.getId(), 0L));
        };
        // 列表映射
        List<BlogAuditGroupDto> list = CopyUtils.mapList(blogAuditGroupList, BlogAuditGroupDto.class, consumer);
        // 封装返回
        return pageDto.rebuild(list);
    }

    public BlogAuditGroupPageEnumDto getBlogAuditGroupPageEnum() {

        // 获取页面参数
        String cacheKey = CacheKeyUtils.getParamCacheKey(EntityConstant.BLOG_AUDIT_GROUP);
        BlogAuditGroupFormVo formVo = cacheHelper.get(cacheKey, BlogAuditGroupFormVo.class);

        BlogAuditGroupPageEnumDto blogAuditGroupPageEnumDto = new BlogAuditGroupPageEnumDto();
        blogAuditGroupPageEnumDto.setAuditTypeList(AuditTypeEnum.getList())
                .setAuditLevelList(AuditLevelEnum.getList())
                .setFormVo(formVo);
        return blogAuditGroupPageEnumDto;
    }

    public void updateBlogAuditGroup(BlogAuditGroupVo vo) {

        BlogAuditGroupEntity updateBlogAuditGroup = new BlogAuditGroupEntity();
        updateBlogAuditGroup.setId(vo.getId())
                .setName(vo.getName());
        blogAuditGroupService.saveOrEdit(updateBlogAuditGroup);

    }

    @Transactional
    public void deleteBlogAuditGroup(List<Long> idList) {

        /*
            删除博客审核分组
         */
        blogAuditGroupService.delete(idList);

        /*
            删除博客审核分组用户
         */
        blogAuditGroupUserService.delete(null, idList);

    }
}
